package com.atm.bulksms;

import java.util.Enumeration;
import java.util.Vector;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.atm.bulksms.data.ContactItem;
import com.atm.bulksms.data.BulkSMSData.GroupData;
import com.atm.bulksms.util.Constants;
import com.atm.bulksms.util.Utils;
public class GroupContactListDialog extends Dialog{
	RadioGroup group;
	 String title;
	 int dialogType ;
	 public static final int TYPE_CREATE_GROUP = 0;
	 public static final int TYPE_EDIT_GROUP = 1;
	public GroupContactListDialog(final GroupListActivity groupListActivity, int type) {
		super(groupListActivity);
		this.dialogType = type;
		
		if(type == TYPE_EDIT_GROUP){
			setContentView(R.layout.group_contact_list);
		}else{
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.group_create_dialog);
			ImageView add = (ImageView) findViewById(R.id.group_add_btn);
			add.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent selectContact = new Intent(groupListActivity, ContactChooser.class);
					selectContact.putExtra(Constants.LIST_TYPE, Constants.LIST_TYPE_CREATE_GROUP);
					groupListActivity.startActivityForResult(selectContact, 0);
				}
			});
		}
		group = (RadioGroup)findViewById(R.id.phone_number_group);
		
		inflator = LayoutInflater.from(groupListActivity);
		//updateGroup(groupListActivity, title, cursor);
		Button doneBtn = (Button)findViewById(R.id.btn_done);
		doneBtn.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				if(dialogType == TYPE_CREATE_GROUP){
					EditText nameView = (EditText)findViewById(R.id.group_name_text);
					String name = nameView.getText().toString();
					System.out.println(" Name "+name);
					if(!TextUtils.isEmpty(name)){
						Cursor c = groupListActivity.getContentResolver().query(GroupData.CONTENT_URI, null, GroupData.GROUP_NAME+"=\""+name+"\"", null, null);
						System.out.println("c.getCount() :: "+c.getCount());
						// check if the name is already exist in database
						if(c.getCount() > 0){
							// already exist
							Bundle bundle = new Bundle();
							bundle.putString(Constants.TITLE, "Alert");
							bundle.putString(Constants.MESSAGE, "The group already exist. please enter a different name.");
							groupListActivity.showDialog(Constants.DIALOG_MESSAGE, bundle);
						}else{
							System.out.println(" contacts "+contacts);
							for(int i = 0; i<contacts.size(); i++){
								System.out.println(" contacts.elementAt(i) "+contacts.elementAt(i));
								if(removedContacts.contains(contacts.elementAt(i))) {
									System.out.println(" continue...");
									continue;
								}
								ContentValues values = new ContentValues();
								values.put(GroupData.GROUP_NAME, name);
								values.put(GroupData.PHONE_NUMBER, contacts.elementAt(i));
								groupListActivity.getContentResolver().insert(GroupData.CONTENT_URI, values);
							}
							
							Bundle bundle = new Bundle();
							bundle.putString(Constants.TITLE, "Info");
							bundle.putString(Constants.MESSAGE, "The group is saved successfully.");
							groupListActivity.showDialog(Constants.DIALOG_MESSAGE,bundle);
							
							Utils.removeAllContacts();
							c.close();
							cancel();
						}
					}
				} else {
					// save the content into the database
					// update the database...
					for (int i = 0; i < removedContacts.size(); i++) {
						groupListActivity.getContentResolver().delete(
								GroupData.CONTENT_URI,
							
	GroupData.GROUP_NAME + "=\"" + title
										+ "\" AND " + GroupData.PHONE_NUMBER
										+ "=\"" + removedContacts.elementAt(i)
										+ "\"", null);
					}
				
	cancel();
					if (group.getChildCount() == 0) {
						groupListActivity
								.showDialog(GroupListActivity.DIALOG_GROUP_REMOVED);
					} else {
						groupListActivity
								.showDialog(GroupListActivity.DIALOG_GROUP_SAVED);
					}
				}
				
			}
			});
	}
	Vector<String> contacts = new Vector<String>();
	LayoutInflater inflator;
	public void updateGroup(Context context, String title, Cursor cursor) {
		this.title = title;
		group.removeAllViews();
		contacts.removeAllElements();
		removedContacts.removeAllElements();
		setTitle(title);
		System.out.println(" Name :: "+title);
		System.out.println(" Count "+cursor.getCount());
		while(cursor.moveToNext()){
			final RelativeLayout layout = (RelativeLayout)inflator.inflate(R.layout.group_contact_item, null);
			TextView name = (TextView)layout.findViewById(R.id.group_contact_item_name);
			final String no = cursor.getString(cursor.getColumnIndex(GroupData.PHONE_NUMBER));
			contacts.add(no);
			name.setText(Utils.getContactName(context, no));
			group.addView(layout);
			ImageView btn = (ImageView) layout.findViewById(R.id.group_contact_item_btn);
			btn.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View v) {
					group.removeView(layout);
					removedContacts.add(no);
				}});
			System.out.println(" Added "+group.getChildCount());
		}
		//group.invalidate();
		System.out.println(" contacts "+contacts);
	
	}
	
	public void updateGroup() {
		System.out.println(" UpdateGroup ");
		group.removeAllViews();
		System.out.println(" removing all the contacts..");
		contacts.removeAllElements();
		removedContacts.removeAllElements();
		//contacts = new Vector<String>();
		//removedContacts  = new Vector<String>();
		//setTitle(title);
		Enumeration<ContactItem> items = Utils.getSelectedContacts().elements();
		while(items.hasMoreElements()){
			ContactItem item = items.nextElement();
			Vector<String> numberList = item.getNumberList();
			for(int i = 0; i<numberList.size(); i++){
				final RelativeLayout layout = (RelativeLayout)inflator.inflate(R.layout.group_contact_item, null);
				TextView name = (TextView)layout.findViewById(R.id.group_contact_item_name);
				final String no = numberList.elementAt(i);
				contacts.add(no);
				name.setText(item.getName()+"\n"+no);
				group.addView(layout);
				ImageView btn = (ImageView) layout.findViewById(R.id.group_contact_item_btn);
				btn.setOnClickListener(new View.OnClickListener(){

					@Override
					public void onClick(View v) {
						group.removeView(layout);
						removedContacts.add(no);
					}});
				System.out.println(" Added "+group.getChildCount());
			
			}
		}
			System.out.println(" Contacts "+contacts);	

	}
	
	Vector<String> removedContacts= new Vector<String>();
	
	
	

}
